package de.msspp.btservice;

import java.util.List;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class BluetoothDeviceAdapter extends ArrayAdapter<BluetoothDevice> {
	
	private List<BluetoothDevice> btList;

	public BluetoothDeviceAdapter(Context context, int textViewResourceId,
			List<BluetoothDevice> objects) {
		super(context, textViewResourceId, objects);
		btList = objects;
	}
	
	/** Check if the passed Device already exists in our List */
	public Boolean contains(BluetoothDevice d){
		if(btList.contains(d))
			return true;
		else
			return false;
	}
	
	/** Custom getView() method for display of BluetoothDevices in ListView */
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		if(convertView == null)
		{
			LayoutInflater vi = (LayoutInflater)super.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = vi.inflate(R.layout.row, null);
		}
		BluetoothDevice device = super.getItem(position);
		if(device != null){
			TextView tName = (TextView)convertView.findViewById(R.id.deviceName);
			TextView tPaired = (TextView)convertView.findViewById(R.id.devicePaired);
			if(tName != null){
				tName.setText(device.getName());
			}
			if(tPaired != null){
				switch(device.getBondState()){
				case BluetoothDevice.BOND_NONE:
					tPaired.setText("Unpaired");
					break;
				case BluetoothDevice.BOND_BONDING:
					tPaired.setText("Pairing");
					break;
				case BluetoothDevice.BOND_BONDED:
					tPaired.setText("Paired");
					break;
				}
			}
 		}
		return convertView;
	}

}
